classdef ET_LUMINANCE < ET_GEN
    
    properties
        Fixation
        fix = struct('w',4, ... Fixation width in degrees.
            'h',4, ... fixation width in degrees
            'rcolor', [0 125 0], ... % Reference color
            'vcolor', [125 0 0], ...  % Color that is variated
            'vd',1, ... % Which dimension is variated, either 1,2,3 (r,g,b);
            'shape','FillOval')
        bg = [0 0 0] % Background
        ff = 2; % Flip frequency
        bh_r
        flip_t = zeros(1e4,1)
    end
    
    methods
        
        function startGM(self)
            % Initializes the graphical motor, i.e., cogent or PTB.
            % Prevents input into matlab
            %ListenChar(2);
            
            % Makes the keyboard insensible to capslock pressing
            [self.scn.p] = Screen('OpenWindow',0);
            
            self.set_clut()                      
            % Initiliazies the screen an saves the pointer into scn.p
            Screen('FillRect',self.scn.p,self.bg);
            Screen(self.scn.p, 'Flip');
            if self.EyeLink
                HideCursor();
            end
            % Wait to have any input key pressed
            self.centeredText('Press a key when ready...');
            KbEventFlush();
            WaitSecs(0.3);
            while ~KbCheck();
            end
            WaitSecs(0.3);
        end
        
        
        function runScreen(self)
            % Runs the screen.
            
            % Start dialog
            self.startDialog()
            
            % Goes to a high priority level
            % Priority(1) How to use this without making a massive
            % dissaster? What are the differences of using Priority in
            % different OS (windows and mac os?).
            
            % Main loop going through blocks and trials
            self.startGM();
            KbEventFlush();
            
            sp_ = @(cx,w,h) [ cx - w/2,self.scn.h/2 - h/2,cx ...
                + w/2,self.scn.h/2 + h/2];
            
            % degrees to pixels
            d2p_ = @(d) round(self.scn.dist*tand(d)*(self.scn.wcm));
            
            % Delta of the color
            if isunix()
                Priority(1);
            elseif ispc()
                priorityLevel=MaxPriority(self.scn.p);
                Priority(priorityLevel);
            end
            dl = 0;
            q = KbName('q');
            up = KbName('up');
            down = KbName('down');
            smp = sp_(self.scn.w/2,d2p_(self.fix.w),d2p_(self.fix.h));
            nc = self.fix.vcolor;
            i = 1;
            l = 0;
            t0 = Screen('Flip',self.scn.p);
            count = 1;
            ff = self.ff - 0.4;
            while 1
                ct = GetSecs();
                if ct - t0 > ff/self.scn.refresh_rate
                    if i 
                        if  any(nc(self.fix.vd) + dl > 255) || ...
                                any(nc(self.fix.vd) + dl < 0)
                        else
                            nc(self.fix.vd) = nc(self.fix.vd) + dl;
                        end
                        Screen(self.fix.shape,self.scn.p,nc,round(smp));
                        i = 0;
                    else
                        Screen(self.fix.shape,self.scn.p,self.fix.rcolor,round(smp));
                        i = 1;
                    end
                    t0 = Screen('Flip',self.scn.p);
                    self.flip_t(count) = t0;
                    count = count+1;
                end
                %fprintf(1,'Red %d.\n',nc(self.fix.vd));
                if l == 10000
                    [avail,~,k] = KbCheck();
                    l = 1;
                    if avail
                        k = find(k);
                        if k == KbName('q')
                            KbEventFlush();
                            self.centeredText(['Press ''q'' to quit.' ...
                                ' Press any other key to continue. '])
                            % Buffer.
                            WaitSecs(0.5);
                            while 1
                                [avail,~,k] = KbCheck();
                                if avail;
                                    break
                                end
                            end
                            % If the key is q
                            if find(k) == q
                                self.bh_r = nc;
                                break
                            end
                            KbEventFlush();
                            break
                        elseif k == up
                            dl = 1;
                        elseif k == down
                            dl = -1;
                        else
                            dl = 0;
                        end
                        KbEventFlush();
                    else
                        dl = 0;
                    end
                else
                    l = l + 1;
                end
                
            end
            Priority(0);
            self.exit()
        end
        
        
    end
    
end

